package com.lzyjkz.quick_system.service.impl;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzyjkz.quick_system.entity.SysMenu;
import com.lzyjkz.quick_system.entity.vo.SysMenuNode;
import com.lzyjkz.quick_system.mapper.SysMenuMapper;
import com.lzyjkz.quick_system.service.SysMenuService;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

    private final SysMenuMapper sysMenuMapper;

    @Override
    public List<SysMenuNode> getPage(Page<SysMenu> page, Long fatherId) {
        List<SysMenuNode> sysMenus = null;

        if (ObjectUtil.isEmpty(fatherId)) {
            sysMenus = sysMenuMapper.getPage(page);
        } else {
            sysMenus = sysMenuMapper.getByFatherId(fatherId);
        }

        return sysMenus;
    }

    @Override
    @Transactional
    public boolean recursiveDeleteById(Long id) {
        List<SysMenu> sysMenus = sysMenuMapper.selectList(new LambdaQueryWrapper<SysMenu>().eq(SysMenu::getFatherId, id));
        if (ObjectUtil.isNotEmpty(sysMenus)) {
            for (SysMenu sysMenu : sysMenus) {
                recursiveDeleteById(sysMenu.getId());
            }
        }
        int i = sysMenuMapper.deleteById(id);
        return NumberUtil.compare(i, 0) != 0;
    }

}
